﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using MSH.Common.Classes;
using WP7Client.Helpers;
using WP7Client.ServerManager;
using MSH.Common.DAO.Results;

namespace WP7Client.Controller
{
    public class ChangeMobileNumberController : BaseController
    {
        #region Members

        private String _Password;
        private String _MobilePhoneNumber;

        #endregion

        #region Get&Set

        public ICommand ChangeCommand 
        { 
            get; 
            internal set; 
        }

        public String Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public String MobilePhoneNumber
        {
            get { return _MobilePhoneNumber; }
            set { _MobilePhoneNumber = value; }
        }


        #endregion

        public ChangeMobileNumberController()
        {
            ChangeCommand = new RelayCommand(ChangeExecute);
        }

        /// <summary>
        /// Wijzigen van het mobiele nummer
        /// </summary>
        private void ChangeExecute()
        {
            if (String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(MobilePhoneNumber))
            {
                GlobalHelper.ShowMessage("Error", "All fields need to be filled.");
                return;
            } 

            Loading = true;
            HttpGet resetHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "ChangeMobilePhoneNumberRequest/{0}/{1}/{2}", UserHelper.Token, MobilePhoneNumber, GlobalHelper.MD5(Password)), typeof(ChangeMobilePhoneNumberRequestResult));
            resetHttpGet.FinishedGet += resetHttpGet_FinishedGet;
        }

        void resetHttpGet_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return; //Eventueel foutmelding

                ChangeMobilePhoneNumberRequestResult result = (ChangeMobilePhoneNumberRequestResult)get.resultObject;

                switch (result.Result)
                {
                    case ChangeMobilePhoneNumberRequestResultEnum.UserNotExists:
                        GlobalHelper.ShowMessage("Error", "User does not exists. \nPlease try again.");
                        break;
                    case ChangeMobilePhoneNumberRequestResultEnum.Succes:
                        GlobalHelper.ShowMessage("Information", "Your activation code has been sent to your phone.");
                        NavigationHelper.NavigateTo("/UserInterface/Pages/ActivateNewMobileNumberPage.xaml");
                        break;
                    case ChangeMobilePhoneNumberRequestResultEnum.ToManyRequests:
                        GlobalHelper.ShowMessage("Error", "Maximum number of requests reached. \nPlease try to activate with last received activation code.");
                        NavigationHelper.NavigateTo("/UserInterface/Pages/LoginPage.xaml");
                        break;
                    case ChangeMobilePhoneNumberRequestResultEnum.IncorrectPassword:
                        GlobalHelper.ShowMessage("Error", "Incorrect password. \nPlease try again.");
                        break;
                    case ChangeMobilePhoneNumberRequestResultEnum.SameNumber:
                        GlobalHelper.ShowMessage("Error", "Please specify a new mobile phone number.");
                        break;
                }
            }
            finally
            {
                Loading = false;
            }
        }
    }
}
